//
//  UserManager.m
//  EveryDay
//
//  Created by FengDongsheng on 15/6/25.
//  Copyright (c) 2015年 FengDongsheng. All rights reserved.
//

#import "UserManager.h"
#import <objc/runtime.h>
#import "DSUtils.h"
#import <YYModel/YYModel.h>
#import "NSObject+InitWithDictionary.h"
#import "CYLTabBarController.h"

#define USER_MANAGER    @"user_manager"

@implementation UserManager

+ (UserManager*)sharedUserManager{
    static UserManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserManager alloc] init];
        [manager initUser];
    });
    return manager;
}

- (void)initUser{
    UserModel *info = [UserModel yy_modelWithDictionary:[UserDefaults objectForKey:USER_MANAGER]];
    ApplicationModel *runInfo = [[ApplicationModel alloc]init];
    
    self.userInfo = info;
    self.appRunInfo = runInfo;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar *vars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = vars[i];
        const char *name = ivar_getName(ivar);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    free(vars);
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    unsigned int count = 0;
    if (self = [super init]) {
        Ivar *vars = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar var = vars[i];
            const char *name = ivar_getName(var);
            //归档
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(vars);
    }
    return self;
}

- (BOOL)checkLogin:(UIViewController *)viewController{
//    NSString *sessionid = DSStringValue([UserDefaults objectForKey:@"userToken"]);
//    if (sessionid.length == 0) {
//        if (![[DSUtils obtainTopViewController] isKindOfClass:[LoginVC class]]){
//            LoginVC *vcLogin = [[LoginVC alloc] init];
//            UINavigationController *vcNavigation = [[UINavigationController alloc] initWithRootViewController:vcLogin];
//            [viewController presentViewController:vcNavigation animated:YES completion:nil];
//        }
//        return NO;
//    }
    return YES;
}

- (BOOL)isLogin{
    NSString *sessionid = DSStringValue([UserDefaults objectForKey:@"userToken"]);
    if (sessionid.length > 0) {
        return YES;
    }
    return NO;
}

- (void)showLoginPage:(UIViewController *)viewController{
//    if (![[DSUtils obtainTopViewController] isKindOfClass:[LoginVC class]]){
//        [self logout];
//        LoginVC *vcLogin = [[LoginVC alloc] init];
//        UINavigationController *vcNavigation = [[UINavigationController alloc] initWithRootViewController:vcLogin];
//        [viewController presentViewController:vcNavigation animated:YES completion:nil];
//    }
}

- (void)checkSetPhone:(UIViewController *)viewController{
    
}

//退出登录
- (void)logout{
    [self removeAllUserData];
}

- (void)removeAllUserData {
    [UserDefaults setObject:@"" forKey:@"userToken"];
    [UserDefaults setObject:@"" forKey:@"loginType"];
    [UserDefaults setObject:@"" forKey:@"userPhone"];
    [UserDefaults setObject:@"" forKey:@"iconUrl"];
    [UserDefaults setObject:@"" forKey:@"userName"];
    [UserDefaults setObject:@"" forKey:USER_MANAGER];
    [UserDefaults synchronize];
}

-(void)updateAddress:(NSString *)pro with:(NSString *)city{
    pro = [pro stringByReplacingOccurrencesOfString:@"省" withString:@""];
    city = [city stringByReplacingOccurrencesOfString:@"市" withString:@""];
    if ([DSStringValue(pro) isEqualToString:@""] || [DSStringValue(pro) isEqualToString:@"中国"]) {
        if ([city isEqualToString:@"上海"]) {
             [UserDefaults setObject:@"上海"  forKey:@"pro"];
        }
        if ([city isEqualToString:@"天津"]) {
            [UserDefaults setObject:@"天津"  forKey:@"pro"];
        }
        if ([city isEqualToString:@"北京"]) {
            [UserDefaults setObject:@"北京"  forKey:@"pro"];
        }
        if ([city isEqualToString:@"重庆"]) {
            [UserDefaults setObject:@"重庆"  forKey:@"pro"];
        }
        
    }
    [UserDefaults setObject:DSStringValue(city) forKey:@"city"];
    [UserDefaults synchronize];
}

//登录
- (void)updateUserInfo:(NSDictionary *)uInfo{
    self.userInfo = [UserModel yy_modelWithDictionary:uInfo];
    [UserDefaults setObject:self.userInfo.userToken forKey:@"userToken"];
    [UserDefaults setObject:self.userInfo.loginType forKey:@"loginType"];
    [UserDefaults setObject:self.userInfo.userPhone forKey:@"userPhone"];
    [UserDefaults setObject:self.userInfo.iconUrl forKey:@"iconUrl"];
    [UserDefaults setObject:self.userInfo.userName forKey:@"userName"];
    [UserDefaults setObject:uInfo forKey:USER_MANAGER];
    [UserDefaults synchronize];
}

//更新用户信息
-(void)updateUser{
    NSData *data = [self.userInfo yy_modelToJSONData];
    NSDictionary  *response =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    [UserDefaults setObject:response forKey:USER_MANAGER];
    NSLog(@"%@",[UserDefaults objectForKey:USER_MANAGER]);
    [UserDefaults synchronize];
}


@end
